{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vacancy in a Silicon Cluster - Python version\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../temp')\n",
    "\n",
    "from julip import JulipCalculator, JulipOptimizer\n",
    "from ase.build import bulk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# reference energy\n",
    "#   energy per unit volume of a homogeneous silicon crystal \n",
    "at = bulk(\"Si\")\n",
    "calc = JulipCalculator(\"JuLIP.Potentials.StillingerWeber()\")\n",
    "at.set_calculator(calc)\n",
    "Eref = at.get_potential_energy()\n",
    "Eref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# cluster with vacancy\n",
    "at = bulk(\"Si\", cubic=True) * (4, 4, 4)\n",
    "del at[len(at)/2]\n",
    "at.set_calculator(calc)\n",
    "\n",
    "# energy before relaxing\n",
    "E0 = at.get_potential_energy()\n",
    "at0 = at.copy()\n",
    "E0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "opt = JulipOptimizer(at)\n",
    "results = opt.run(fmax=1e-6)\n",
    "E1 = at.get_potential_energy()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# defect formation energy \n",
    "print \"Vacancy Formation Energy without relaxing: \", E0 - (len(at)+1)*Eref/2\n",
    "print \"Vacancy Formation Energy   with  relaxing: \", E1 - (len(at)+1)*Eref/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# geometry optimisation\n",
    "from ase.optimize import LBFGSLineSearch\n",
    "at = at0.copy()\n",
    "at.set_calculator(calc)\n",
    "opt = LBFGSLineSearch(at)\n",
    "opt.run(1e-3)\n",
    "E1 = at.get_potential_energy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# defect formation energy \n",
    "print \"Vacancy Formation Energy without relaxing: \", E0 - (len(at)+1)*Eref/2\n",
    "print \"Vacancy Formation Energy   with  relaxing: \", E1 - (len(at)+1)*Eref/2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
